package com.example.customviewtest.wymusic;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.customviewtest.R;
import com.google.gson.Gson;

import java.util.List;

public class WYMusicActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemClickListener {

    private RecyclerView mRv_music;
    private List<MusicBean.AlbumBean> mAlbum;
    private List<MusicBean.HotBean> mHot;
    private WYListAdapter mWyListAdapter;
    private RecyclerView mGrid_view;
    private WYGridAdapter mWyGridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wymusic);
        mRv_music = findViewById(R.id.rv_music);
        String jsonFiletoString = AssetsUtil.getJsonFiletoString("DataSource.json", this);
        Gson gson = new Gson();
        MusicBean musicBean = gson.fromJson(jsonFiletoString, MusicBean.class);

        mAlbum = musicBean.getAlbum();
        mHot = musicBean.getHot();

        mRv_music.setLayoutManager(new LinearLayoutManager(this));
        mWyListAdapter = new WYListAdapter(this, R.layout.wy_list_item, mHot);
        mRv_music.setAdapter(mWyListAdapter);

        View head_view = LayoutInflater.from(this).inflate(R.layout.wy_head_layout, null);
        mGrid_view = head_view.findViewById(R.id.grid_view);
        mGrid_view.setLayoutManager(new GridLayoutManager(this, 3));
        mGrid_view.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.left = 4;
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) parent.getLayoutParams();
                layoutParams.leftMargin = -4;
                parent.setLayoutParams(layoutParams);
            }
        });
        mWyGridAdapter = new WYGridAdapter(this, R.layout.wy_grid_item, mAlbum);
        mGrid_view.setAdapter(mWyGridAdapter);
        mWyGridAdapter.setOnItemClickListener(this);
        mWyListAdapter.addHeaderView(head_view);
        mWyListAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (adapter instanceof WYListAdapter) {
            MusicBean.HotBean hotBean = mHot.get(position);
            Intent intent = new Intent(WYMusicActivity.this, PlayMusicActivity.class);
            intent.putExtra("author", hotBean.getAuthor());
            intent.putExtra("name", hotBean.getName());
            intent.putExtra("poster", hotBean.getPoster());
            intent.putExtra("path", hotBean.getPath());
            startActivity(intent);

        }
        if (adapter instanceof WYGridAdapter) {
            MusicBean.AlbumBean albumBean = mAlbum.get(position);
            Intent intent = new Intent(WYMusicActivity.this, WYMusicListActivity.class);
            intent.putParcelableArrayListExtra("list", albumBean.getList());
            startActivity(intent);
        }
    }
}
